
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_deer/account/iview/takeout_list_iview.dart';
import 'package:flutter_deer/account/models/user_score_takeout_entity.dart';
import 'package:flutter_deer/account/presenter/takeout_list_presenter.dart';
import 'package:flutter_deer/account/provider/takeout_list_provider.dart';
import 'package:flutter_deer/mvp/base_page.dart';
import 'package:flutter_deer/mvp/power_presenter.dart';
import 'package:flutter_deer/res/gaps.dart';
import 'package:flutter_deer/util/other_utils.dart';
import 'package:flutter_deer/widgets/fadein_text.dart';
import 'package:flutter_deer/widgets/my_card.dart';
import 'package:flutter_deer/widgets/my_refresh_list.dart';
import 'package:flutter_deer/widgets/state_layout.dart';
import 'package:flutter_gen/gen_l10n/deer_localizations.dart';
import 'package:intl/intl.dart';
import 'package:lottie/lottie.dart';
import 'package:provider/provider.dart';

class TakeoutListListPage extends StatefulWidget {

  final int index;
  const TakeoutListListPage({super.key,required this.index});

  @override
  _TakeoutListListPageState createState() => _TakeoutListListPageState();
}

class _TakeoutListListPageState extends State<TakeoutListListPage> with BasePageMixin<TakeoutListListPage, PowerPresenter>, AutomaticKeepAliveClientMixin<TakeoutListListPage>,SingleTickerProviderStateMixin implements TakeoutListIMvpView {

  late TakeoutListPresenter takeoutListPresenter;
  TakeoutListProvider provider = TakeoutListProvider();

  @override
  PowerPresenter<dynamic> createPresenter() {
    final PowerPresenter<dynamic> powerPresenter = PowerPresenter<dynamic>(this);
    takeoutListPresenter = new TakeoutListPresenter();
    powerPresenter.requestPresenter([takeoutListPresenter]);
    return powerPresenter;
  }

  @override
  void setTakeoutPage(UserScoreTakeoutPageEntity? pageEntity){
    provider.setTakeoutPage(pageEntity);
  }

  @override
  void initState() {
    super.initState();
    if(mounted){
      _onRefresh();
    }
  }

  @override
  void dispose() {
    super.dispose();
  }

  Future<dynamic> _onRefresh() async {
    Future.delayed(Duration(seconds: 1),()=>{takeoutListPresenter.fetchList(widget.index.toString())});
  }

  Future<dynamic> _loadMore() async {
    takeoutListPresenter.getMoreTakeOutList(widget.index.toString(), provider.currentPage!+1);
  }

  @override
  Widget build(BuildContext context) {
    super.build(context);
    return ChangeNotifierProvider<TakeoutListProvider>(
        create: (_) => provider,
        child: Consumer<TakeoutListProvider>(
            builder: (_, provider, child) {
              if(provider.stateType==StateType.loading){
                return StateLayout(type: provider.stateType!);
              }
              if(provider.list!.length>0) {
                return DeerListView(
                    itemCount: provider.list!.length!,
                    stateType: provider.stateType!,
                    onRefresh: _onRefresh,
                    loadMore: _loadMore,
                    hasMore: provider.hasMore!,
                    itemBuilder: (_, index) {
                      final UserScoreTakeoutEntity item = provider.list!
                          .elementAt(index);
                      return Padding(
                          padding: const EdgeInsets.symmetric(
                              horizontal: 10.0, vertical: 5),
                          child: MyCard(
                              child: Padding(
                                  padding: const EdgeInsets.symmetric(
                                      horizontal: 5),
                                  child: Column(
                                      crossAxisAlignment: CrossAxisAlignment
                                          .start,
                                      children: [
                                        SizedBox(height: 5,),
                                        Row(
                                          mainAxisAlignment: MainAxisAlignment
                                              .spaceBetween,
                                          children: [
                                            Text('${DeerLocalizations.of(context)!.withdrawAmount.nullSafe}:' +
                                                NumberFormat.decimalPattern('en_us').format(item!.score).toString()),
                                            Text('${DeerLocalizations.of(context)!.withdrawTimes.nullSafe}:' +
                                                item!.createTimeStr.nullSafe)
                                          ],
                                        ),
                                        SizedBox(height: 5,),
                                        Divider(height: 1.0, indent: 0.0),
                                        SizedBox(height: 5,),
                                        Row(
                                          mainAxisAlignment: MainAxisAlignment
                                              .spaceBetween,
                                          children: [
                                            Text('${DeerLocalizations.of(context)!.bankName.nullSafe}:' +
                                                item!.bankName.nullSafe),
                                            Text(
                                                '${DeerLocalizations.of(context)!.bankCardNumber.nullSafe}:' + item!.cardNo.nullSafe)
                                          ],
                                        ),
                                        SizedBox(height: 10,),
                                        Row(
                                          mainAxisAlignment: MainAxisAlignment
                                              .spaceBetween,
                                          children: [
                                            Text('${DeerLocalizations.of(context)!.cardholder.nullSafe}:' +
                                                item!.bankUserName.nullSafe)
                                          ],
                                        ),
                                        Visibility(
                                          visible: item!.status != '0',
                                          child: SizedBox(height: 10,),
                                        ),
                                        Visibility(
                                          visible: item!.status != '0',
                                          child: Row(
                                            mainAxisAlignment: MainAxisAlignment
                                                .spaceBetween,
                                            children: [
                                              Text(
                                                  '${DeerLocalizations.of(context)!.remark.nullSafe}:' + item!.remark.nullSafe)
                                            ],
                                          ),
                                        ),
                                        Visibility(
                                          visible: item!.status != '0',
                                          child: SizedBox(height: 5,),
                                        ),
                                        Visibility(
                                          visible: item!.status != '0',
                                          child: Divider(
                                              height: 1.0, indent: 0.0),
                                        ),
                                        Visibility(
                                          visible: item!.status != '0',
                                          child: SizedBox(height: 5,),
                                        ),
                                        Visibility(
                                            visible: item!.status != '0',
                                            child: Row(
                                              mainAxisAlignment: MainAxisAlignment
                                                  .spaceBetween,
                                              children: [
                                                Text(''),
                                                Text('${DeerLocalizations.of(context)!.auditTime.nullSafe}:' +
                                                    item!.approvalTimeStr
                                                        .nullSafe)
                                              ],
                                            )
                                        ),
                                        SizedBox(height: 5,),
                                      ]
                                  )
                              )
                          )
                      );
                    }
                );
              }else{
                return Container(
                  width: MediaQuery.of(context).size.width,
                  child: Center(
                    child: Column(
                      mainAxisAlignment: MainAxisAlignment.center,
                      crossAxisAlignment: CrossAxisAlignment.center,
                      children: [
                        Lottie.asset('assets/lottie/cash_in.json',width: MediaQuery.of(context).size.width/3,fit: BoxFit.cover),
                        Gaps.vGap8,
                        FadeInText(text: '${DeerLocalizations.of(context)!.noWithdrawInformation.nullSafe}')
                      ],
                    ),
                  ),
                );
              }
            }
        )
    );
  }

  @override
  bool get wantKeepAlive => false;

}